package plantObjects;

import control.Event;
import control.Eventhandler;
import java.util.ArrayList;
import java.util.LinkedList;

/**
 * Crane at the beginning of the system
 * Fixed time for every slap - can/must be updated to var time
 * @author guru
 */

public class CraneStart implements TransformingUnit
{
    private int time;
    // first in, first out lists (LinkedList)
    private LinkedList<Position> positions = new LinkedList<Position>();
    private LinkedList<String> barTypes = new LinkedList<String>();

    CraneStart(int time)
    {
        this.time = time;
    }
/**
 * Empty, because slap is allways on storage
 * @param s
 */
    public void put(Slap s) { }

    /**
     * get the current slap from crane
     * @return
     */
    public Slap get()
    {
        // create net event, if there is a crane order left an working list
        if(positions.size()>1) Eventhandler.add(new Event(Eventhandler.getTime()+time, this));
        else System.out.println("StartCrane is idle");
        // get & remove order from top of lists
        Position p = positions.poll();
        return new Slap(barTypes.poll());
    }
/**
 * order new slaps from start storage
 * @param positions of slaps / their barType
 */

    public void orderSlap(ArrayList<Position> positions, String barType)
    {
        //if there were no other orders left, create a new event for the crane
        if(this.positions.isEmpty()) Eventhandler.add(new Event(Eventhandler.getTime()+time, this));

        // add Positions to internal pos list
        this.positions.addAll(positions);

        // add barTypes to working list and create events when slap is ready to take of
        for(int i = 0; i < positions.size(); i++)
        {
            this.barTypes.add(barType);
        }
    }
    /**
     * Checks if crane is unused & available
     * @return true if available / false if it has something todo
     */
    public boolean isAvail()
    {
        return positions.isEmpty();
    }
}
